<?php
namespace Admin\Controller;
use Think\Controller;
class AuthController extends CommonController {
    //Auth认证管理
    public function index(){
        //获取用户信息
        $user=D("user")->relation(true)->field("password",true)->select();
        $this->user=$user;
        //获取用户组信息
        $group=M("auth_group")->select();
        $obj=M("auth_rule");
        foreach($group as $k=>$v){
            $map['id'] = array('in',$group[$k]['rules']);
            $group[$k]['group']=$obj->where($map)->select();
        }
        $this->group=$group;
        //获取rule规则
        $this->rule=M("auth_rule")->select();
        //$ip = new \Org\Net\IpLocation("UTFWry.dat");
        //$location=$ip->getlocation();
        //p($location);die;
        //p($group);die;
        $this->display();
    }
    //添加后台用户及表单处理
    public function user(){
        if(IS_POST){
            $data=array(
                'username'=>I('username','','trim'),
                'remark'=>I('remark','','trim'),
                'password'=>I('password','','md5'),
                'status'=>I('status',0,'intval'),
                'rsgtime'=>$_SERVER['REQUEST_TIME'],
                'login_num'=>0
            );
            if(!isset($_POST['role_id'])){
                $this->error("请选择用户组...");
            }
            if($data['password']!=md5($_POST['repassword'])){
                $this->error("两次密码不一致...");
            }
            if(M("user")->where(array('username'=>$data['username']))->find()){
                $this->error("用户名已存在...");
            }
            if($lastInsertId=M("user")->add($data)){
                foreach($_POST['role_id'] as $k=>$v){
                    $arr=array(
                        'uid'=>$lastInsertId,
                        'group_id'=>$_POST['role_id'][$k]
                    );
                    M("auth_group_access")->add($arr);
                }
                M("auth_group_access")->add($arr);
                $this->success("添加成功...",U("Auth/index"));
            }else{
                $this->error("添加失败...");
            }
        }else{
            $this->group=M("auth_group")->field("id,title")->select();
            $this->display();
        }

    }
    //添加后台用户组及表单处理
    public function group(){
        if(IS_POST){
            $data=array(
                'title'=>I('title','','trim'),
                'status'=>I('status',0,'intval')
            );
            if(M("auth_group")->where(array('title'=>$data['title']))->find()){
                $this->error("用户组名称已存在...");
            }
            if(M("auth_group")->add($data)){
                $this->success("添加成功...",U("Auth/index"));
            }else{
                $this->error("添加失败...");
            }
        }else{
            $this->display();
        }
    }
    //添加后台权限及表单处理
    public function auth(){
        if(IS_POST){
            $data=array(
                'name'=>I('name','','trim'),
                'title'=>I('title','','trim'),
                'condition'=>I('condition','','trim'),
                'status'=>I('status',0,'intval'),
                'type'=>I('type',0,'intval'),
            );
            if(M("auth_rule")->add($data)){
                $this->success("添加成功...",U("Auth/index"));
            }else{
                $this->error("添加失败...");
            }
        }else{
            $this->display();
        }
    }
    //注册rule规则
    public function register(){
        $class_name=get_class();
        return register($class_name);
    }
    //删除用户组
    public function deleteGroup(){
        if(IS_GET){
            if(!isset($_GET['id'])){
                return false;
            }
            $id=I("id",0,"intval");
            if(!$id){
                return false;
            }
            if(M("auth_group")->where(array("id"=>$id))->delete()){
                M("auth_group_access")->where(array("group_id"=>$id))->delete();
                $this->success("删除成功...",U("Auth/index"));
            }else{
                $this->error("删除失败...");
            }
        }
    }
    //修改用户组
    public function modifyGroup(){
        if(IS_GET){
            if(!isset($_GET['id'])){
                return false;
            }
            $id=I("id",0,"intval");
            if(!$id){
                return false;
            }
            $group=M("auth_group")->where(array("id"=>$id))->find();
            $rule=M("auth_rule")->select();
            foreach($rule as $k=>$v){
                if(in_array($rule[$k]['id'],explode(',',$group['rules']))){
                    $rule[$k]['is_checked']=1;
                }else{
                    $rule[$k]['is_checked']=0;
                }
            }
            $this->rule=$rule;
            $this->group=$group;
            $this->id=$id;
            //p($rule);die;
            $this->display();
        }elseif(IS_POST){
            $data=array(
                'title'=>I("title","","trim"),
                'rules'=>implode(",",$_POST['rule']),
                'status'=>I("status","","trim")
            );
            if(M("auth_group")->where(array("id"=>$_POST['id']))->save($data)){
                $this->success("修改成功...",U("Auth/index"));
            }else{
                $this->error("修改失败...");
            }
        }
    }
    //删除RULE
    public function deleteRule(){
        if(IS_GET){
            if(!isset($_GET['id'])){
                return false;
            }
            $id=I("id",0,"intval");
            if(!$id){
                return false;
            }
            if(M("auth_rule")->where(array("id"=>$id))->delete()){
                $this->success("删除成功...",U("Auth/index"));
            }else{
                $this->error("删除失败...");
            }
        }
    }
    //修改RULE
    public function modifyRule(){
        if(IS_POST){
            if(!isset($_POST['id'])){
                return false;
            }
            $id=I("id",0,"intval");
            unset($_POST['id']);
            if(!$id){
                return false;
            }
            if(M("auth_rule")->where(array("id"=>$id))->save($_POST)){
                $this->success("修改成功...",U("Auth/index"));
            }else{
                $this->error("修改失败...");
            }
        }
    }
    //删除用户
    public function deleteUser(){
        if(IS_GET){
            if(!isset($_GET['id'])){
                return false;
            }
            $id=I("id",0,"intval");
            if(!$id){
                return false;
            }
            if(M("user")->where(array("id"=>$id))->delete()){
                M("auth_group_access")->where(array("uid"=>$id))->delete();
                $this->success("删除成功...",U("Auth/index"));
            }else{
                $this->error("删除失败...");
            }
        }
    }
    //修改用户
    public function modifyUser(){
        if(IS_POST){
            $id=I("id",0,"intval");
            if(!$id){
                return false;
            }
            $data['username']=I("username","","trim");
            $data['remark']=I("remark","","trim");
            $data['status']=I("status",0,"intval");
            $tmp=0;
            if(isset($_POST['role_id'])){
                M("auth_group_access")->where(array("uid"=>$id))->delete();
                foreach ($_POST['role_id'] as $key => $value) {
                    if(M("auth_group_access")->add(array("uid"=>$id,"group_id"=>$_POST['role_id'][$key]))){
                        $tmp=1;
                    }
                }
            }
            if(trim($_POST['password'])){
                if(md5($_POST['password'])!=md5($_POST['repassword'])){
                    $this->error("两次密码输入不一致...");
                }else{
                    $data['password']=I("password","","md5");
                }
            }
            if(M("user")->where(array("id"=>$id))->save($data)){
                $this->success("修改成功...",U("Auth/index"));
            }else{
                if($tmp){
                    $this->success("修改成功...",U("Auth/index"));
                }else{
                    $this->error("修改失败...");
                }
            }
        }elseif(IS_GET){
            if(!isset($_GET['id'])){
                return false;
            }
            $id=I("id",0,"intval");
            if(!$id){
                return false;
            }
            $this->user=M("user")->where(array('id'=>$id))->field("password",true)->find();
            $user_group=M("auth_group_access")->where(array("uid"=>$id))->select();
            $group=M("auth_group")->select();
            foreach($user_group as $key => $value){
                $user_group[$key]['group_name']=M("auth_group")->where(array("id"=>$user_group[$key]['group_id']))->getField("title");
            }
            $this->user_group=$user_group;
            $this->group=$group;
            $this->display();
        }
    }
}